#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "file.h"
#include "pack.h"
#include "tcp.h"

void list_file(char *str)
{
	DIR *dirp;
	char filepath[300];
	struct dirent *envdir;
	struct stat stbuf;
	int len = 0;
	memset(str, 0, 1024);

	dirp = opendir("/home/ysh/Desktop/tcpmsg/server/bin/lib");		//打开服务器文件夹
	if(dirp == NULL)
	{
		printf("opendir error\n");
	}
	while((envdir = readdir(dirp)) != NULL)							//未读取完目录下条目 循环
	{
		memset(filepath, 0, sizeof(filepath));
		sprintf(filepath, "/home/ysh/Desktop/tcpmsg/server/bin/lib/%s", envdir -> d_name);	//文件绝对路径
		stat(filepath, &stbuf);										//获取文件信息
		if(S_ISDIR(stbuf.st_mode))									//判断为目录
		{
			continue;
		}
		len += sprintf(str+len, "%s:", envdir -> d_name);			//填入文件名
	}
	closedir(dirp);													//关闭文件夹描述符
}

void *send_file(void *argv)										//文件发送线程
{
	int fd,index, flen, sockfd;
	unsigned long pktId;
	char filepath[300], buf[1024], filebuf[1024], sendbuf[1024];
	pthread_detach(pthread_self());
	pktId = (*(Arg *)argv).pktId;
	sockfd = (*(Arg *)argv).sockfd;
	
	index = find_sFiles(sockfd, pktId);							//获取发送序列
	sprintf(filepath,"%s%s", "/home/ysh/Desktop/tcpmsg/server/bin/lib/", sFiles[index].filename);
	fd = open(filepath, O_RDONLY);
	if(fd == -1)
		printf("open file error\n");

	pack_010(buf, index);										//发送文件名
	write(sockfd, buf, 1024);

	lseek(fd, sFiles[index].offset, SEEK_SET);					//偏移文件描述符
	memset(filebuf, 0, sizeof(filebuf));
	memset(sendbuf, 0, sizeof(sendbuf));
	while((flen = read(fd, filebuf, 994)) > 0)					//读取文件内容
	{
		pack_011(sendbuf, filebuf, index, flen);				//组合数据包
		write(sockfd, sendbuf, sizeof(sendbuf));				
		memset(filebuf, 0, sizeof(filebuf));
		memset(sendbuf, 0, sizeof(sendbuf));
	}

	pack_012(buf, index);										//发送结束
	write(sockfd, buf, 1024);

	close(fd);											//关闭文件描述符
	clean_sFiles(index);								//清空序列
	return 0;
}

void creat_file(int index)								//建立接收文件
{
	int fd, i = 1;
	char filepath[300];
	sprintf(filepath, "./bin/lib/%s", rFiles[index].filename);
	if(access("./bin/lib", F_OK) == -1)					//检验是否存在服务器文件夹
		mkdir("./bin/lib", 0755);						//建立文件夹
	while(access(filepath, F_OK) == 0)					//检验文件是否存在
	{
		sprintf(filepath, "./bin/lib/_%d_%s", i, rFiles[index].filename);	//已存在则更换文件名
		i++;
	}
	fd = open(filepath, O_WRONLY | O_CREAT, 0666);		//打开文件描述符
	rFiles[index].status = 0;							//填充序列
	rFiles[index].fileId = fd;
}

void init_Files()					//初始化队列
{
	int i = 0;
	for(i = 0; i < 10 ; i++)
	{
		clean_sFiles(i);
		clean_rFiles(i);
	}
}
//获取发送序列
int get_sFiles()
{
	int i = 0;
	for(i = 0; i < 10 ; i++)
	{
		if(sFiles[i].sockfd == -1)
			break;
	}
	return i;
}
//获取接收序列
int get_rFiles()
{
	int i = 0;
	for(i = 0; i < 10 ; i++)
	{
		if(rFiles[i].pktId == 0)
			break;
	}
	return i;
}
//寻找发送序列
int find_sFiles(int sockfd, unsigned long pktId)
{
	int i = 0;
	for(i = 0; i < 10 ; i++)
	{
		if(sFiles[i].sockfd == sockfd && sFiles[i].pktId == pktId)
			break;
	}
	return i;
}
//寻找接收序列
int find_rFiles(int sockfd, unsigned long pktId)
{
	int i = 0;
	for(i = 0; i < 10 ; i++)
	{
		if(rFiles[i].sockfd == sockfd && rFiles[i].pktId == pktId)
			break;
	}
	return i;
}
//清理发送序列
void clean_sFiles(int index)
{
	memset(sFiles[index].filename, 0, sizeof(sFiles[index].filename));
	sFiles[index].offset = 0;
	sFiles[index].fileSize = 0;
	sFiles[index].sockfd = -1;
}
//清理接收序列
void clean_rFiles(int index)
{
	memset(rFiles[index].filename, 0, sizeof(rFiles[index].filename));
	rFiles[index].offset = 0;
	rFiles[index].fileSize = 0;
	rFiles[index].sockfd = -1;
	rFiles[index].status = -1;
}